package com.whenjun.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ifootbook.util.*;
import com.whenjun.dto.kq.*;
import com.whenjun.entity.IscDoorRecord;
import com.whenjun.entity.IscPerson;
import com.whenjun.entity.KqAlarm;
import com.whenjun.enums.KqAlarmTypeEnum;
import com.whenjun.enums.KqDoorTypeEnum;
import com.whenjun.mapper.KqAlarmMapper;
import com.whenjun.service.IKqAlarmService;
import com.whenjun.util.ExportExcelUtils;
import com.whenjun.util.SaTokenUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author whenjun
 * @since 2024-02-26
 */
@Service
public class KqAlarmServiceImpl extends MPJBaseServiceImpl<KqAlarmMapper, KqAlarm> implements IKqAlarmService {

    @Autowired
    private KqConfigServiceImpl kqConfigService;

    @Autowired
    private IscDoorRecordServiceImpl iscDoorRecordService;

    @Autowired
    private IscPersonServiceImpl iscPersonService;

    @Transactional
    public IPage<KqAlarmDTO> KqAlarms(KqAlarmReq kqAlarmReq) {
        Page page = new Page(kqAlarmReq.getPage(), kqAlarmReq.getSize());

        LocalDateTime begin = null;
        LocalDateTime end = null;

        if (!EmptyUtil.isEmpty(kqAlarmReq.getDate())) {
            LocalDateTime dateTime = LocalDateTimeUtil.parse(kqAlarmReq.getDate(), "yyyy-MM-dd");
            begin = LocalDateTimeUtil.beginOfDay(dateTime);
            end = LocalDateTimeUtil.endOfDay(dateTime);
        }

        MPJLambdaWrapper<KqAlarm> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(KqAlarm.class)
                .select(IscPerson::getPersonName)
                .leftJoin(IscPerson.class, IscPerson::getJobNo, KqAlarm::getJobNo)
                .apply("1=1")
                .eq(!EmptyUtil.isEmpty(kqAlarmReq.getTypeCode()), KqAlarm::getTypeCode, kqAlarmReq.getTypeCode())
                .ge(!EmptyUtil.isEmpty(begin), KqAlarm::getCreateTime, begin)
                .le(!EmptyUtil.isEmpty(end), KqAlarm::getCreateTime, end)
                .and(qr -> qr.apply("1=1").like(!EmptyUtil.isEmpty(kqAlarmReq.getKeywords()), IscPerson::getPersonName, kqAlarmReq.getKeywords())
                        .or()
                        .like(!EmptyUtil.isEmpty(kqAlarmReq.getKeywords()), KqAlarm::getJobNo, kqAlarmReq.getKeywords())
                        .or()
                        .like(!EmptyUtil.isEmpty(kqAlarmReq.getKeywords()), KqAlarm::getReason, kqAlarmReq.getKeywords()))

                .orderByDesc(KqAlarm::getCreateTime);

        IPage<KqAlarmDTO> kqAlarmDTOIPage = this.getBaseMapper().selectJoinPage(page, KqAlarmDTO.class, wrapper);
        return kqAlarmDTOIPage;
    }

    @Transactional
    public void KqAlarmExport(String monStr, HttpServletResponse response) throws ParseException {
        CheckedUtil.isEmpty(monStr, "monStr为空");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateStyle.YYYY_MM.getValue());
        Date monDate = simpleDateFormat.parse(monStr);
        LocalDateTime beginDate = TimeUtil.getLocalDateTime(DateUtil.beginOfMonth(monDate));
        LocalDateTime endDate = TimeUtil.getLocalDateTime(DateUtil.endOfMonth(monDate));

        MPJLambdaWrapper<KqAlarm> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(KqAlarm.class)
                .select(IscPerson::getPersonName)
                .leftJoin(IscPerson.class, IscPerson::getJobNo, KqAlarm::getJobNo)
                .ge(KqAlarm::getCreateTime, beginDate)
                .le(KqAlarm::getCreateTime, endDate)
                .orderByDesc(KqAlarm::getCreateTime);
        List<KqAlarmDTO> kqAlarmDTOS = this.getBaseMapper().selectJoinList(KqAlarmDTO.class, wrapper);

        //文件名
        String excelName = monStr + "月" + "报警数据.xls";

        String sheetName = "报警数据";
        List<LinkedHashMap<String, String>> fieldMapList = new ArrayList<LinkedHashMap<String,String>>();
        //sheet表头字段名
        LinkedHashMap<String, String> fieldMap =new LinkedHashMap<String, String>();
        fieldMap.put("jobNo", "工号");
        fieldMap.put("personName", "姓名");
        fieldMap.put("reason", "考勤异常原因");
        fieldMap.put("exceptionTime", "考勤异常时间（秒）");
        fieldMap.put("fixState", "修正状态");
        fieldMap.put("fixTime", "修正时间");

        //导出
        ExportExcelUtils.export(excelName,sheetName, kqAlarmDTOS, fieldMap, response);
    }

    @Transactional
    public IPage<KqDTO> Kqs(KqReq kqReq) {
        IPage<KqDTO> iPage = new Page<>(kqReq.getPage(), kqReq.getSize());
        KqDTO kqDTO1 = new KqDTO();
        kqDTO1.setPersonId("1932a231a79746e4a0e5efb731c27927");
        kqDTO1.setJobNo("1083");
        kqDTO1.setPersonName("VIP1 (MAN)");
        kqDTO1.setDepartmentId("1");
        kqDTO1.setDepartmentName("部门一");
        kqDTO1.setDepartmentGroupId("d8f9dd7933db4594a3814f2fb803f891");
        kqDTO1.setDepartmentGroupName("12小时班组");
        kqDTO1.setKqState("1");
        kqDTO1.setDakaShangbanNum(1);
        kqDTO1.setDakaXiabanNum(2);
        kqDTO1.setJiabanTime(3600);
//        kqDTO1.setKqDatetime(LocalDateTime.now());
        kqDTO1.setCountKqTime(9999999);
        kqDTO1.setRestSumTime(655);
        kqDTO1.setRemark("remark");

        KqDTO kqDTO2 = new KqDTO();
        kqDTO2.setPersonId("1932a231a79746e4a0e5efb731c27927");
        kqDTO2.setJobNo("1083");
        kqDTO2.setPersonName("VIP1 (MAN)");
        kqDTO2.setDepartmentId("1");
        kqDTO2.setDepartmentName("部门一");
        kqDTO2.setDepartmentGroupId("d8f9dd7933db4594a3814f2fb803f891");
        kqDTO2.setDepartmentGroupName("12小时班组");
        kqDTO2.setKqState("1");
        kqDTO2.setDakaShangbanNum(1);
        kqDTO2.setDakaXiabanNum(2);
        kqDTO2.setJiabanTime(3600);
//        kqDTO2.setKqDatetime(LocalDateTime.now());
        kqDTO2.setCountKqTime(9999999);
        kqDTO2.setRestSumTime(655);
        kqDTO2.setRemark("remark");

        List<KqDTO> kqDTOS = new ArrayList<>();
        kqDTOS.add(kqDTO1);
        kqDTOS.add(kqDTO2);

        iPage.setRecords(kqDTOS);
        iPage.setPages(1);
        iPage.setCurrent(1);
        iPage.setSize(10);
        iPage.setTotal(2);

        return iPage;
    }


    @Transactional
    public void KqAlarmUpd(KqAlarmUpdReq kqAlarmUpdReq) {
        KqAlarm kqAlarm = this.getById(kqAlarmUpdReq.getId());
        if (kqAlarm == null) {
            throw new FootBookException("id错误");
        }
        LocalDateTime now = LocalDateTime.now();
        String userId = SaTokenUtil.getUserId();
        kqAlarm.setExceptionTime(kqAlarmUpdReq.getUpdTime());
        kqAlarm.setFixState(true);
        kqAlarm.setFixTime(now);
        kqAlarm.setFixUser(userId);

        this.updateById(kqAlarm);
    }

    // 计算通道时间，如果通道时间小于配置的时间，则不计入休息时间，否则计入
    @Transactional
    public void calcFloorRestTime(IscDoorRecord iscDoorRecord) {
        Boolean isTongdao = iscDoorRecordService.checkIndexCodeInGroupType(iscDoorRecord.getSrcIndex(), KqDoorTypeEnum.tongdao.getCode());

        // 判断是否通道门禁 以及 inOut是否为出 = 1 以及 personId不为空
        // 1进    2出   2025-03-12
        if (isTongdao == true
                && iscDoorRecord.getInOut() == 1
                && EmptyUtil.isEmpty(iscDoorRecord.getPersonId())
        ) {
            // 配置中通道时间
            Integer tongdaoTime = kqConfigService.KqConfig().getFloorRestTime();

            // 当前时间
            LocalDateTime now = LocalDateTime.now();

            // 查询此人，上次进入通道的时间 可能需要判断inOut TODO
            LastRecordReq lastRecordReq = new LastRecordReq();
            lastRecordReq.setHappenTime(iscDoorRecord.getHappenTime());
            lastRecordReq.setPersonId(iscDoorRecord.getPersonId());
            lastRecordReq.setSrcIndex(iscDoorRecord.getSrcIndex());

            IscDoorRecord lastRecoed = iscDoorRecordService.getBaseMapper().selectLastRecordBySrcIndexAndPersonId(lastRecordReq);
            // 异常情况可能为空，仅处理不为空的情况
            if (lastRecoed != null) {
                Duration between = LocalDateTimeUtil.between(lastRecoed.getHappenTime(), iscDoorRecord.getHappenTime());

                Integer currentTime = (int) between.getSeconds();

                // 记录 通道超时报警，报警code 对应 KqAlarmTypeEnum
                if (currentTime >= tongdaoTime) {

                    // isc_person add
                    IscPerson iscPerson = iscPersonService.lambdaQuery().eq(IscPerson::getPersonId, lastRecoed.getPersonId()).one();

                    KqAlarm kqAlarm = new KqAlarm();
                    kqAlarm.setCreateTime(now);
                    kqAlarm.setEventEndTime(iscDoorRecord.getHappenTime());
                    kqAlarm.setEventStartTime(lastRecoed.getHappenTime());
                    kqAlarm.setExceptionTime(currentTime);
                    kqAlarm.setJobNo(iscPerson.getJobNo());
                    kqAlarm.setTypeCode(KqAlarmTypeEnum.rest_tongdao.getCode());
                    kqAlarm.setReason(KqAlarmTypeEnum.rest_tongdao.getName());

                    this.save(kqAlarm);
                }
            }else {
                System.out.println("calcFloorRestTime: 无上次记录，查询参数 " + lastRecordReq.toString());
            }
        } else {
            System.out.println("calcFloorRestTime: 无效记录，isTongdao：" + isTongdao + "  inOut:" + iscDoorRecord.getInOut() + "  personId: " + iscDoorRecord.getPersonId());
        }
    }

    @Transactional
    public void calcFloorRestTimeV2(IscDoorRecord iscDoorRecord) {
        Boolean isTongdao = iscDoorRecordService.checkIndexCodeInGroupType(iscDoorRecord.getSrcIndex(), KqDoorTypeEnum.tongdao.getCode());

        // 判断是否通道门禁 以及 inOut是否为出 = 1 以及 personId不为空
        // 1进    2出   2025-03-12
        // 出休息区：查询进通道的时间，计算通道时长
        // 进非休息区：查询进通道的时间，计算通道时长
        if (isTongdao == true
                && iscDoorRecord.getInOut() == 2
                && EmptyUtil.isEmpty(iscDoorRecord.getPersonId())
        ) {
            // 配置中通道时间
            Integer tongdaoTime = kqConfigService.KqConfig().getFloorRestTime();

            // 当前时间
            LocalDateTime now = LocalDateTime.now();


            IscDoorRecord lastRecoed = iscDoorRecordService.getBaseMapper().selectLastRecordByPersonIdAndGroupType(iscDoorRecord.getPersonId(), KqDoorTypeEnum.tongdao.getCode());
            // 异常情况可能为空，仅处理不为空的情况
            if (lastRecoed != null) {
                Duration between = LocalDateTimeUtil.between(lastRecoed.getHappenTime(), iscDoorRecord.getHappenTime());

                Integer currentTime = (int) between.getSeconds();

                // 记录 通道超时报警，报警code 对应 KqAlarmTypeEnum
                if (currentTime >= tongdaoTime) {

                    // isc_person add
                    IscPerson iscPerson = iscPersonService.lambdaQuery().eq(IscPerson::getPersonId, lastRecoed.getPersonId()).one();

                    KqAlarm kqAlarm = new KqAlarm();
                    kqAlarm.setCreateTime(now);
                    kqAlarm.setEventEndTime(iscDoorRecord.getHappenTime());
                    kqAlarm.setEventStartTime(lastRecoed.getHappenTime());
                    kqAlarm.setExceptionTime(currentTime);
                    kqAlarm.setJobNo(iscPerson.getJobNo());
                    kqAlarm.setTypeCode(KqAlarmTypeEnum.rest_tongdao.getCode());
                    kqAlarm.setReason(KqAlarmTypeEnum.rest_tongdao.getName());

                    this.save(kqAlarm);
                }
            }else {
                System.out.println("calcFloorRestTime: 无上次记录，查询参数 , personId: " + iscDoorRecord.getPersonId() + "，Doortype:" + KqDoorTypeEnum.tongdao.getCode());
            }
        } else if (isTongdao == false
                && iscDoorRecord.getInOut() == 1
                && EmptyUtil.isEmpty(iscDoorRecord.getPersonId())
        ) {
            // 进非通道区：查询进通道的时间，计算通道时长
            // 配置中通道时间
            Integer tongdaoTime = kqConfigService.KqConfig().getFloorRestTime();

            // 当前时间
            LocalDateTime now = LocalDateTime.now();

            LastRecordByPersonIdAndInGroupTypeReq lastRecordByPersonIdAndInGroupTypeReq = new LastRecordByPersonIdAndInGroupTypeReq();
            lastRecordByPersonIdAndInGroupTypeReq.setPersonId(iscDoorRecord.getPersonId());
            List<String> groupTypes = new ArrayList<>();
            groupTypes.add(KqDoorTypeEnum.shangban.getCode());
            groupTypes.add(KqDoorTypeEnum.xiaban.getCode());
            groupTypes.add(KqDoorTypeEnum.huwai.getCode());
            groupTypes.add(KqDoorTypeEnum.waichu.getCode());
            lastRecordByPersonIdAndInGroupTypeReq.setGroupTypes(groupTypes);

            IscDoorRecord lastRecoed = iscDoorRecordService.getBaseMapper().selectLastRecordByPersonIdAndInGroupType(lastRecordByPersonIdAndInGroupTypeReq);

            // 异常情况可能为空，仅处理不为空的情况
            if (lastRecoed != null) {
                Duration between = LocalDateTimeUtil.between(lastRecoed.getHappenTime(), iscDoorRecord.getHappenTime());

                Integer currentTime = (int) between.getSeconds();

                // 记录 通道超时报警，报警code 对应 KqAlarmTypeEnum
                if (currentTime >= tongdaoTime) {

                    // isc_person add
                    IscPerson iscPerson = iscPersonService.lambdaQuery().eq(IscPerson::getPersonId, lastRecoed.getPersonId()).one();

                    KqAlarm kqAlarm = new KqAlarm();
                    kqAlarm.setCreateTime(now);
                    kqAlarm.setEventEndTime(iscDoorRecord.getHappenTime());
                    kqAlarm.setEventStartTime(lastRecoed.getHappenTime());
                    kqAlarm.setExceptionTime(currentTime);
                    kqAlarm.setJobNo(iscPerson.getJobNo());
                    kqAlarm.setTypeCode(KqAlarmTypeEnum.rest_tongdao.getCode());
                    kqAlarm.setReason(KqAlarmTypeEnum.rest_tongdao.getName());

                    this.save(kqAlarm);
                }
            }else {
                System.out.println("calcFloorRestTime: 无上次记录，查询参数 , personId: " + iscDoorRecord.getPersonId() + "，Doortype:" + KqDoorTypeEnum.tongdao.getCode());
            }
        }

        else {
            System.out.println("calcFloorRestTime: 无效记录，isTongdao：" + isTongdao + "  inOut:" + iscDoorRecord.getInOut() + "  personId: " + iscDoorRecord.getPersonId());
        }
    }


    // 计算wc时间，如果通道时间小于配置的时间，则不计入休息时间，否则计入
    @Transactional
    public void calcWcRestTime(IscDoorRecord iscDoorRecord) {
        Boolean isWc = iscDoorRecordService.checkIndexCodeInGroupType(iscDoorRecord.getSrcIndex(), KqDoorTypeEnum.wc.getCode());

        // 判断是否通道门禁 以及 inOut是否为出 = 1 以及 personId不为空
        if (isWc == true
                && iscDoorRecord.getInOut() == 1
                && EmptyUtil.isEmpty(iscDoorRecord.getPersonId())
        ) {
            // 配置中wc时间
            Integer wcTime = kqConfigService.KqConfig().getWcTime();

            // 当前时间
            LocalDateTime now = LocalDateTime.now();

            // 查询此人，上次进入通道的时间 ,可能需要判断inOut TODO
            LastRecordReq lastRecordReq = new LastRecordReq();
            lastRecordReq.setHappenTime(iscDoorRecord.getHappenTime());
            lastRecordReq.setPersonId(iscDoorRecord.getPersonId());
            lastRecordReq.setSrcIndex(iscDoorRecord.getSrcIndex());

            IscDoorRecord lastRecoed = iscDoorRecordService.getBaseMapper().selectLastRecordBySrcIndexAndPersonId(lastRecordReq);
            // 异常情况可能为空，仅处理不为空的情况
            if (lastRecoed != null) {
                Duration between = LocalDateTimeUtil.between(lastRecoed.getHappenTime(), iscDoorRecord.getHappenTime());

                Integer currentTime = (int) between.getSeconds();

                // 记录 通道超时报警，报警code 对应 KqAlarmTypeEnum
                if (currentTime >= wcTime) {

                    // isc_person add
                    IscPerson iscPerson = iscPersonService.lambdaQuery().eq(IscPerson::getPersonId, lastRecoed.getPersonId()).one();

                    KqAlarm kqAlarm = new KqAlarm();
                    kqAlarm.setCreateTime(now);
                    kqAlarm.setEventEndTime(iscDoorRecord.getHappenTime());
                    kqAlarm.setEventStartTime(lastRecoed.getHappenTime());
                    kqAlarm.setExceptionTime(currentTime);
                    kqAlarm.setJobNo(iscPerson.getJobNo());
                    kqAlarm.setTypeCode(KqAlarmTypeEnum.rest_wc.getCode());
                    kqAlarm.setReason(KqAlarmTypeEnum.rest_wc.getName());

                    this.save(kqAlarm);
                }
            }else {
                System.out.println("calcWcRestTime: 无上次记录，查询参数 " + lastRecordReq.toString());
            }
        } else {
            System.out.println("calcWcRestTime: 无效记录，isWc：" + isWc + "  inOut:" + iscDoorRecord.getInOut() + "  personId: " + iscDoorRecord.getPersonId());
        }
    }

    @Transactional
    public void calcWcRestTimeV2(IscDoorRecord iscDoorRecord) {
        Boolean isWc = iscDoorRecordService.checkIndexCodeInGroupType(iscDoorRecord.getSrcIndex(), KqDoorTypeEnum.wc.getCode());

        // 判断是否通道门禁 以及 inOut是否为出 = 1 以及 personId不为空
        // 1进    2出   2025-03-12
        // 出休息区：查询进通道的时间，计算通道时长
        // 进非休息区：查询进通道的时间，计算通道时长
        if (isWc == true
                && iscDoorRecord.getInOut() == 2
                && EmptyUtil.isEmpty(iscDoorRecord.getPersonId())
        ) {
            // 配置中通道时间
            Integer tongdaoTime = kqConfigService.KqConfig().getWcTime();

            // 当前时间
            LocalDateTime now = LocalDateTime.now();


            IscDoorRecord lastRecoed = iscDoorRecordService.getBaseMapper().selectLastRecordByPersonIdAndGroupType(iscDoorRecord.getPersonId(), KqDoorTypeEnum.wc.getCode());
            // 异常情况可能为空，仅处理不为空的情况
            if (lastRecoed != null) {
                Duration between = LocalDateTimeUtil.between(lastRecoed.getHappenTime(), iscDoorRecord.getHappenTime());

                Integer currentTime = (int) between.getSeconds();

                // 记录 通道超时报警，报警code 对应 KqAlarmTypeEnum
                if (currentTime >= tongdaoTime) {

                    // isc_person add
                    IscPerson iscPerson = iscPersonService.lambdaQuery().eq(IscPerson::getPersonId, lastRecoed.getPersonId()).one();

                    KqAlarm kqAlarm = new KqAlarm();
                    kqAlarm.setCreateTime(now);
                    kqAlarm.setEventEndTime(iscDoorRecord.getHappenTime());
                    kqAlarm.setEventStartTime(lastRecoed.getHappenTime());
                    kqAlarm.setExceptionTime(currentTime);
                    kqAlarm.setJobNo(iscPerson.getJobNo());
                    kqAlarm.setTypeCode(KqAlarmTypeEnum.rest_wc.getCode());
                    kqAlarm.setReason(KqAlarmTypeEnum.rest_wc.getName());

                    this.save(kqAlarm);
                }
            }else {
                System.out.println("calcWcRestTime: 无上次记录，查询参数 , personId: " + iscDoorRecord.getPersonId() + "，Doortype:" + KqDoorTypeEnum.tongdao.getCode());
            }
        } else if (isWc == false
                && iscDoorRecord.getInOut() == 1
                && EmptyUtil.isEmpty(iscDoorRecord.getPersonId())
        ) {
            // 进非通道区：查询进通道的时间，计算通道时长
            // 配置中通道时间
            Integer tongdaoTime = kqConfigService.KqConfig().getFloorRestTime();

            // 当前时间
            LocalDateTime now = LocalDateTime.now();

            LastRecordByPersonIdAndInGroupTypeReq lastRecordByPersonIdAndInGroupTypeReq = new LastRecordByPersonIdAndInGroupTypeReq();
            lastRecordByPersonIdAndInGroupTypeReq.setPersonId(iscDoorRecord.getPersonId());
            List<String> groupTypes = new ArrayList<>();
            groupTypes.add(KqDoorTypeEnum.shangban.getCode());
            groupTypes.add(KqDoorTypeEnum.xiaban.getCode());
            groupTypes.add(KqDoorTypeEnum.huwai.getCode());
            groupTypes.add(KqDoorTypeEnum.waichu.getCode());
            lastRecordByPersonIdAndInGroupTypeReq.setGroupTypes(groupTypes);

            IscDoorRecord lastRecoed = iscDoorRecordService.getBaseMapper().selectLastRecordByPersonIdAndInGroupType(lastRecordByPersonIdAndInGroupTypeReq);

            // 异常情况可能为空，仅处理不为空的情况
            if (lastRecoed != null) {
                Duration between = LocalDateTimeUtil.between(lastRecoed.getHappenTime(), iscDoorRecord.getHappenTime());

                Integer currentTime = (int) between.getSeconds();

                // 记录 通道超时报警，报警code 对应 KqAlarmTypeEnum
                if (currentTime >= tongdaoTime) {

                    // isc_person add
                    IscPerson iscPerson = iscPersonService.lambdaQuery().eq(IscPerson::getPersonId, lastRecoed.getPersonId()).one();

                    KqAlarm kqAlarm = new KqAlarm();
                    kqAlarm.setCreateTime(now);
                    kqAlarm.setEventEndTime(iscDoorRecord.getHappenTime());
                    kqAlarm.setEventStartTime(lastRecoed.getHappenTime());
                    kqAlarm.setExceptionTime(currentTime);
                    kqAlarm.setJobNo(iscPerson.getJobNo());
                    kqAlarm.setTypeCode(KqAlarmTypeEnum.rest_wc.getCode());
                    kqAlarm.setReason(KqAlarmTypeEnum.rest_wc.getName());

                    this.save(kqAlarm);
                }
            }else {
                System.out.println("calcWcRestTime: 无上次记录，查询参数 , personId: " + iscDoorRecord.getPersonId() + "，Doortype:" + KqDoorTypeEnum.wc.getCode());
            }
        }

        else {
            System.out.println("calcWcRestTime: 无效记录，isWc：" + isWc + "  inOut:" + iscDoorRecord.getInOut() + "  personId: " + iscDoorRecord.getPersonId());
        }
    }

    // 计算单次休息区时间，如果时间小于配置的时间，则不计入休息时间，否则计入
    @Transactional
    public void calcOneRestTime(IscDoorRecord iscDoorRecord) {
        Boolean isRestArea = iscDoorRecordService.checkIndexCodeInGroupType(iscDoorRecord.getSrcIndex(), KqDoorTypeEnum.xiuxi.getCode());

        // 判断是否通道门禁 以及 inOut是否为出 = 1 以及 personId不为空
        if (isRestArea == true
                && iscDoorRecord.getInOut() == 1
                && EmptyUtil.isEmpty(iscDoorRecord.getPersonId())
        ) {
            // 配置中wc时间
            Integer singleRestTime = kqConfigService.KqConfig().getSingleRestTime();

            // 当前时间
            LocalDateTime now = LocalDateTime.now();

            // 查询此人，上次进入休息区的时间 ,可能需要判断inOut TODO
            LastRecordReq lastRecordReq = new LastRecordReq();
            lastRecordReq.setHappenTime(iscDoorRecord.getHappenTime());
            lastRecordReq.setPersonId(iscDoorRecord.getPersonId());
            lastRecordReq.setSrcIndex(iscDoorRecord.getSrcIndex());

            IscDoorRecord lastRecoed = iscDoorRecordService.getBaseMapper().selectLastRecordBySrcIndexAndPersonId(lastRecordReq);
            // 异常情况可能为空，仅处理不为空的情况
            if (lastRecoed != null) {
                Duration between = LocalDateTimeUtil.between(lastRecoed.getHappenTime(), iscDoorRecord.getHappenTime());

                Integer currentTime = (int) between.getSeconds();

                // 记录 通道超时报警，报警code 对应 KqAlarmTypeEnum
                if (currentTime >= singleRestTime) {

                    // isc_person add
                    IscPerson iscPerson = iscPersonService.lambdaQuery().eq(IscPerson::getPersonId, lastRecoed.getPersonId()).one();

                    KqAlarm kqAlarm = new KqAlarm();
                    kqAlarm.setCreateTime(now);
                    kqAlarm.setEventEndTime(iscDoorRecord.getHappenTime());
                    kqAlarm.setEventStartTime(lastRecoed.getHappenTime());
                    kqAlarm.setExceptionTime(currentTime);
                    kqAlarm.setJobNo(iscPerson.getJobNo());
                    kqAlarm.setTypeCode(KqAlarmTypeEnum.rest_one.getCode());
                    kqAlarm.setReason(KqAlarmTypeEnum.rest_one.getName());

                    this.save(kqAlarm);
                }
            }else {
                System.out.println("calcOneRestTime: 无上次记录，查询参数 " + lastRecordReq.toString());
            }
        } else {
            System.out.println("calcOneRestTime: 无效记录，isRestArea：" + isRestArea + "  inOut:" + iscDoorRecord.getInOut() + "  personId: " + iscDoorRecord.getPersonId());
        }
    }

    @Transactional
    public void calcOneRestTimeV2(IscDoorRecord iscDoorRecord) {
        Boolean isRestArea = iscDoorRecordService.checkIndexCodeInGroupType(iscDoorRecord.getSrcIndex(), KqDoorTypeEnum.xiuxi.getCode());

        // 判断是否通道门禁 以及 inOut是否为出 = 1 以及 personId不为空
        // 1进    2出   2025-03-12
        // 出休息区：查询进通道的时间，计算通道时长
        // 进非休息区：查询进通道的时间，计算通道时长
        if (isRestArea == true
                && iscDoorRecord.getInOut() == 2
                && EmptyUtil.isEmpty(iscDoorRecord.getPersonId())
        ) {
            // 配置中通道时间
            Integer tongdaoTime = kqConfigService.KqConfig().getWcTime();

            // 当前时间
            LocalDateTime now = LocalDateTime.now();


            IscDoorRecord lastRecoed = iscDoorRecordService.getBaseMapper().selectLastRecordByPersonIdAndGroupType(iscDoorRecord.getPersonId(), KqDoorTypeEnum.xiuxi.getCode());
            // 异常情况可能为空，仅处理不为空的情况
            if (lastRecoed != null) {
                Duration between = LocalDateTimeUtil.between(lastRecoed.getHappenTime(), iscDoorRecord.getHappenTime());

                Integer currentTime = (int) between.getSeconds();

                // 记录 通道超时报警，报警code 对应 KqAlarmTypeEnum
                if (currentTime >= tongdaoTime) {

                    // isc_person add
                    IscPerson iscPerson = iscPersonService.lambdaQuery().eq(IscPerson::getPersonId, lastRecoed.getPersonId()).one();

                    KqAlarm kqAlarm = new KqAlarm();
                    kqAlarm.setCreateTime(now);
                    kqAlarm.setEventEndTime(iscDoorRecord.getHappenTime());
                    kqAlarm.setEventStartTime(lastRecoed.getHappenTime());
                    kqAlarm.setExceptionTime(currentTime);
                    kqAlarm.setJobNo(iscPerson.getJobNo());
                    kqAlarm.setTypeCode(KqAlarmTypeEnum.rest_one.getCode());
                    kqAlarm.setReason(KqAlarmTypeEnum.rest_one.getName());

                    this.save(kqAlarm);
                }
            }else {
                System.out.println("calcWcRestTime: 无上次记录，查询参数 , personId: " + iscDoorRecord.getPersonId() + "，Doortype:" + KqDoorTypeEnum.tongdao.getCode());
            }
        } else if (isRestArea == false
                && iscDoorRecord.getInOut() == 1
                && EmptyUtil.isEmpty(iscDoorRecord.getPersonId())
        ) {
            // 进非通道区：查询进通道的时间，计算通道时长
            // 配置中通道时间
            Integer tongdaoTime = kqConfigService.KqConfig().getFloorRestTime();

            // 当前时间
            LocalDateTime now = LocalDateTime.now();

            LastRecordByPersonIdAndInGroupTypeReq lastRecordByPersonIdAndInGroupTypeReq = new LastRecordByPersonIdAndInGroupTypeReq();
            lastRecordByPersonIdAndInGroupTypeReq.setPersonId(iscDoorRecord.getPersonId());
            List<String> groupTypes = new ArrayList<>();
            groupTypes.add(KqDoorTypeEnum.shangban.getCode());
            groupTypes.add(KqDoorTypeEnum.xiaban.getCode());
            groupTypes.add(KqDoorTypeEnum.huwai.getCode());
            groupTypes.add(KqDoorTypeEnum.waichu.getCode());
            lastRecordByPersonIdAndInGroupTypeReq.setGroupTypes(groupTypes);

            IscDoorRecord lastRecoed = iscDoorRecordService.getBaseMapper().selectLastRecordByPersonIdAndInGroupType(lastRecordByPersonIdAndInGroupTypeReq);

            // 异常情况可能为空，仅处理不为空的情况
            if (lastRecoed != null) {
                Duration between = LocalDateTimeUtil.between(lastRecoed.getHappenTime(), iscDoorRecord.getHappenTime());

                Integer currentTime = (int) between.getSeconds();

                // 记录 通道超时报警，报警code 对应 KqAlarmTypeEnum
                if (currentTime >= tongdaoTime) {

                    // isc_person add
                    IscPerson iscPerson = iscPersonService.lambdaQuery().eq(IscPerson::getPersonId, lastRecoed.getPersonId()).one();

                    KqAlarm kqAlarm = new KqAlarm();
                    kqAlarm.setCreateTime(now);
                    kqAlarm.setEventEndTime(iscDoorRecord.getHappenTime());
                    kqAlarm.setEventStartTime(lastRecoed.getHappenTime());
                    kqAlarm.setExceptionTime(currentTime);
                    kqAlarm.setJobNo(iscPerson.getJobNo());
                    kqAlarm.setTypeCode(KqAlarmTypeEnum.rest_one.getCode());
                    kqAlarm.setReason(KqAlarmTypeEnum.rest_one.getName());

                    this.save(kqAlarm);
                }
            }else {
                System.out.println("calcOneRestTime: 无上次记录，查询参数 , personId: " + iscDoorRecord.getPersonId() + "，Doortype:" + KqDoorTypeEnum.xiuxi.getCode());
            }
        }

        else {
            System.out.println("calcOneRestTime: 无效记录，isRestArea：" + isRestArea + "  inOut:" + iscDoorRecord.getInOut() + "  personId: " + iscDoorRecord.getPersonId());
        }
    }
}
